function AuthorManager (tree, keyArr) {
    this.tree = tree
    this.keyArr = keyArr

    this.getAuthorList = function (tree = this.tree, keyArr = this.formatKey(this.keyArr)) {
        for (let i = 0; i < tree.length; i++) {
            if (tree[i].children) {
                this.getAuthorList(tree[i].children, keyArr)
            }
        }
        this.filterByKeys(tree, keyArr)
        return this.tree
    }

    this.formatKey = function (keyArr) {
        let res = []
        keyArr.forEach((key) => {
            key = key.split('-').map((item, index) => index > 0 ? '-' + item : item)
            key.forEach((item, index) => {
                let term = key.slice(0, index + 1).join('')
                if (!res.includes(term)) {
                    res.push(term)
                }
            })
        })
        return res
    }

    this.filterByKeys = function (item, keyArr) {
        for (var i = 0; i < item.length; i++) {
            if (!keyArr.includes(item[i].key)) {
                item.splice(i, 1)
                i--
            }
        }
    }
}

export default AuthorManager
